local skill = fk.CreateSkill {
  name = "iberia__savage_assault_skill",
}

Fk:loadTranslationTable{
  ["iberia__savage_assault_skill"] = "南蛮入侵",
  ["#iberia__savage_assault_skill"] = "可以令 %arg 对一名目标额外结算一次",
}

local I = require "packages/iberia/utility"

skill:addEffect("cardskill", {
  prompt = "#savage_assault_skill",
  can_use = Util.AoeCanUse,
  mod_target_filter = function(self, player, to_select)
    return to_select ~= player
  end,
  on_use = function (self, room, cardUseEvent)
    ---@cast cardUseEvent -SkillUseData
    Util.AoeCardOnUse(self, cardUseEvent.from, cardUseEvent, false)
    if I.isRepeller(cardUseEvent.from) then
      local to = room:askToChoosePlayers(cardUseEvent.from, {
        max_num = 1,
        min_num = 1,
        targets = cardUseEvent:getAllTargets(),
        skill_name = "iberia__savage_assault",
        cancelable = true,
        prompt = "#iberia__savage_assault-ex"
      })

      if #to == 1 then
        cardUseEvent.extra_data = cardUseEvent.extra_data or {}
        cardUseEvent.extra_data.savage_assault_ex_target = to[1]
      end
    end
  end,
  on_effect = function(self, room, effect)
    local loopTimes = effect:getResponseTimes()
    local use = room.logic:getCurrentEvent().parent
    if use.data.extra_data and use.data.extra_data.savage_assault_ex_target == effect.to then
      loopTimes = loopTimes + 1
    end
    local respond
    for _ = 1, loopTimes do
      local params = { ---@type AskToUseCardParams
        skill_name = 'slash',
        pattern = 'slash',
        cancelable = true,
        event_data = effect
      }
      respond = room:askToResponse(effect.to, params)
      if respond then
        room:responseCard(respond)
      else
        room:damage({
          from = effect.from,
          to = effect.to,
          card = effect.card,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = "iberia__savage_assault:::" .. effect.card:toLogString(),
        })
      end
      if effect.to.dead then break end
    end
  end,
})

return skill
